package com.meila.cms.services.impl;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.meila.cms.dao.UserMapper;
import com.meila.cms.entity.User;
import com.meila.cms.model.CommonModel;
import com.meila.cms.model.ResetPwdModel;
import com.meila.cms.model.UserModel;
import com.meila.cms.services.ISettingService;

@Service
public class SettingService extends BaseService implements ISettingService{
	
	private UserMapper userMapper;
	
	public UserMapper getUserMapper() {
		return userMapper;
	}
	
	@Inject
	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}

	@Override
	public CommonModel saveBaseInfo(User user) {
		// TODO Auto-generated method stub
		CommonModel commonModel=new CommonModel();
		commonModel.setFlag(true);
		commonModel.setMessage("保存成功！");
		
		try{
			UserModel u=userMapper.selectByPhone(user.getPhone());
			u.setUpdateTime(new Date());
			u.setSex(user.getSex());
			u.setCity(user.getCity());
			u.setProvince(user.getProvince());
			userMapper.updateByPrimaryKeySelective(u);
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			
			commonModel.setFlag(false);
			commonModel.setMessage("保存失败！");
		}
		return commonModel;
	}

	@Override
	public CommonModel updatePwd(ResetPwdModel resetPwdModel) {
		// TODO Auto-generated method stub
		CommonModel commonModel=new CommonModel();
		commonModel.setFlag(true);
		commonModel.setMessage("修改成功！");
		
		try{
			UserModel user=getLoginUser();
			if(resetPwdModel.getOpassword().equalsIgnoreCase(user.getPassword())){
				user.setUpdateTime(new Date());
				user.setPassword(resetPwdModel.getNpassword());
				userMapper.updateByPrimaryKeySelective(user);
			}else{
				commonModel.setFlag(false);
				commonModel.setMessage("输入原始密码错误！");
			}
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			
			commonModel.setFlag(false);
			commonModel.setMessage("修改失败！");
		}
		return commonModel;
	}

	@Override
	public CommonModel updatePersonal(UserModel userModel) {
		// TODO Auto-generated method stub
		CommonModel commonModel=new CommonModel();
		
		try{
			UserModel user=userMapper.selectByPhone(userModel.getPhone());
			user.setUpdateTime(new Date());
			user.setName(userModel.getName());
			user.setCompany(userModel.getCompany());
			user.setSite(userModel.getSite());
			user.setLicense(userModel.getLicense());
			user.setIdfront(userModel.getIdfront());
			user.setIdopposite(userModel.getIdopposite());
			user.setWorks1(userModel.getWorks1());
			user.setWorks2(userModel.getWorks2());
			user.setWorks3(userModel.getWorks3());
			user.setAuthentication(3);
			userMapper.updateByPrimaryKeySelective(user);
			
			commonModel.setFlag(true);
			commonModel.setMessage("提交成功！");
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			
			commonModel.setFlag(false);
			commonModel.setMessage("提交失败！");
		}
		return commonModel;
	}

	@Override
	public CommonModel updateFirm(UserModel userModel) {
		// TODO Auto-generated method stub
		CommonModel commonModel=new CommonModel();
		commonModel.setFlag(true);
		commonModel.setMessage("提交成功！");
		
		java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
		
		try{
			UserModel user=userMapper.loadByPhone(userModel.getPhone());
			if(user.getAuthentication()!=1){
				user.setUpdateTime(new Date());
				user.setName(userModel.getName());
				user.setSite(userModel.getSite());
				user.setContacts(userModel.getContacts());
				user.setFixedtel(userModel.getFixedtel());
				user.setEstablishmentDate(formatter.parse(userModel.getEstablishment()));
				user.setScale(userModel.getScale());
				user.setLicense(userModel.getLicense());
				user.setAuthentication(3);
				userMapper.updateByPrimaryKeySelective(user);
			}else{
				commonModel.setFlag(false);
				commonModel.setMessage("提交失败！您的账户已通过认证！");
			}
		}catch(Exception e){
			// TODO: handle exception
			e.printStackTrace();
			
			commonModel.setFlag(false);
			commonModel.setMessage("提交失败！");
		}
		return commonModel;
	}
	
}